<?php

namespace app\modules\Product\models;

use Yii;

/**
 * This is the model class for table "product_log".
 *
 * @property int $id
 * @property int $product_id 团队ID
 * @property string $file_path 报价单
 * @property int $num 次数
 * @property int $status 1 正常 2 禁止
 * @property int $user_id 创建人ID
 * @property int $currency 货币(美元)
 * @property string $price 记录总收入
 * @property string $created_at 创建时间
 * @property string $updated_at
 */
class ProductLog extends \yii\db\ActiveRecord
{
    /**
     * {@inheritdoc}
     */
    public static function tableName()
    {
        return 'product_log';
    }

    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['product_id', 'num', 'status', 'user_id', 'currency'], 'integer'],
            [['price'], 'number'],
            [['created_at', 'updated_at'], 'safe'],
            [['file_path','name'], 'string', 'max' => 255],
            [['ext','size'], 'string', 'max' => 16],
        ];
    }

    //保存前处理
    public function beforeSave($insert) {//{{{

        if($insert){
            $this->created_at = date("Y-m-d H:i:s");
            $this->user_id    = Yii::$app->user->id;

            $count = ProductLog::find()->where(['product_id'=>$this->product_id])->count();

            $this->num = $count+1;
        }
        return parent::beforeSave($insert);

    }//}}}

    /**
     * {@inheritdoc}
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app', 'ID'),
            'product_id' => Yii::t('app', 'Product ID'),
            'file_path' => Yii::t('app', 'File Path'),
            'name' => Yii::t('app', 'Name'),
            'ext' => Yii::t('app', 'Ext'),
            'size' => Yii::t('app', 'Size'),
            'num' => Yii::t('app', 'Num'),
            'status' => Yii::t('app', 'Status'),
            'user_id' => Yii::t('app', 'User ID'),
            'currency' => Yii::t('app', 'Currency'),
            'price' => Yii::t('app', 'Price'),
            'created_at' => Yii::t('app', 'Created At'),
            'updated_at' => Yii::t('app', 'Updated At'),
        ];
    }
}
